<?php
	class ProductCRUDController
	{   
		function save()
		{	
			$id=getFormValue('id');
			if(isset($id))
			{
				$product=R::load('product',$id);
			}
			else
			{
				$product=R::dispense('product');
				$product->typecode=2;
			}
			$product->import($_POST, 'name,sku,seoid,desc,longdesc,online');
			
			$id=R::store($product);
			$_POST=array();
			$_POST['id']=$id;
		}
		
		function delete()
		{	
			$id=getFormValue('id');
			if(isset($id))
			{
				$product=R::load('product',$id);
                               $this->deleteProduct($product);
			}	
		}
		
                private function trash($arr,$table)
                {
                         if(isset($arr)) 
                            {
                               foreach($arr as $k => $obj)
                               {
                                   $temp=R::load($table,$k);
                                   R::trash($temp);
                               }
                            }
                }
                
                private function deleteProduct($product)
                {
                      
                     
                           $this->trash($product->ownProductca,'productca');
                           $this->trash($product->ownProductimage,'productimage');
                           $this->trash($product->ownProductprice,'productprice');
                           $this->trash($product->ownSales,'sales');
                             
                            if($product->typecode == 4)
                            {
                                  $pvs =$product->alias('master')->ownProductvariation ;
                                foreach($pvs as $k => $pv)
                                 {
                                    $variation=$pv->fetchAs('product')->variation;
                                    $this-> deleteProduct($variation) ;  

                                 }
                                  $this->trash($pvs,'productvariation');
                            }
                            $this->trash($product->ownCategory_product,'category_product');
                            
                            R::trash( $product ); //for one bean
                }
                        
		function offline()
		{	
			$id=getFormValue('id');
			if(isset($id))
			{
				$product=R::load('product',$id);
				$product->online=0;
				$id=R::store($product);
			}	
		}
		
		function online()
		{	
			$id=getFormValue('id');
			if(isset($id))
			{
				$product=R::load('product',$id);
				$product->online=1;
				$id=R::store($product);
			}	
		}
		
		function attribute_save()
		{	
			$id=getFormValue('id');
			if(isset($id))
			{
			$product=R::load('product',$id);
			$product->import($_POST, 'brand,partner,color,size,weight,packing');
			$id=R::store($product);
			}
			
		}
		function attribute_ca_save()
		{	
			$id=getFormValue('id');
			if(isset($id))
			{
				$product=R::load('product',$id);
				$productca=R::dispense('productca');
				$productca->import($_POST,'name,value');				
				$productca->product=$product;
				$id=R::store($productca);
			}
			
		}
		
		function attribute_ca_delete()
		{	
			$id=getFormValue('caid');
			if(isset($id))
			{
				$productca=R::load('productca',$id);
				R::trash( $productca ); //for one bean
			}	
		}
		
		function images_save()
		{	
			$id=getFormValue('id');
			if(isset($id))
			{
				$product=R::load('product',$id);
				$productImage=R::dispense('productimage');
				$productImage->import($_POST,'imageview,imagetype');
				$productImage->imagepath='../images/product/' . $_FILES["file"]["name"];
				$productImage->product=$product;
				$id=R::store($productImage);
			}
		}
		
		function images_delete()
		{	
			$id=getFormValue('imageid');
			if(isset($id))
			{
				$productimage=R::load('productimage',$id);
				R::trash( $productimage ); //for one bean
			}	
		}
		
		function prices_save()
		{	
			$id=getFormValue('id');
			if(isset($id))
			{
				$product=R::load('product',$id);
				$productprice=R::dispense('productprice');
				$productprice->import($_POST,'type,price');
				$productprice->product=$product;
				$id=R::store($productprice);
			}
		}
		
		function prices_delete()
		{	
			$id=getFormValue('priceid');
			if(isset($id))
			{
				$productimage=R::load('productprice',$id);
				R::trash( $productimage ); //for one bean
			}	
		}
		
		function promotions_save()
		{	
			$id=getFormValue('id');
			if(isset($id))
			{
				$product=R::load('product',$id);
				$productprice=R::dispense('sales');
				$productprice->import($_POST,'desc,price,start_date,end_date');
				$productprice->product=$product;
				$id=R::store($productprice);
			}
		}
		
		function promotions_delete()
		{	
			$id=getFormValue('promotionid');
			if(isset($id))
			{
				$productimage=R::load('sales',$id);
				R::trash( $productimage ); //for one bean
			}	
		}
		function categories_save()
		{
			$ids=getFormValue('category_ids');			
			$id=getFormValue('id');
			if(isset($ids) && isset($id))
			{
				$product=R::load('product',$id);
				if(is_array($ids))
				{
					 $categories = R::loadAll( 'category', $ids );					
					 $product->sharedCategory=$categories;
				}
				else
				{
					$category =R::load( 'category', $ids );
					 $product->sharedCategory=array($category);
				}
				R::store($product);	
			}
		}
		
		function variations_add()
		{
			$id=getFormValue('id');
			if(isset($id))
			{
				$product=R::load('product',$id);				
				//$copied=$this->copy($product);
				$copied=R::dup( $product );
				$copied->import($_POST,'color,size');
				$copied->sku=$product->sku.'-'.$copied->color.'-'.$copied->size;
				$copied->seoid=$product->seoid.'_'.strtolower($copied->color).'_'.$copied->size;
				$copied->name=$product->name.' : '.$copied->color.' , '.$copied->size;
				$cid=R::store($copied);
				$copied=R::load('product',$cid);
				
				$pv = R::dispense( 'productvariation' );
				$pv->master = $product;
				$pv->variation = $copied;
				$pvid = R::store($pv);

				$copied->typecode=8;
				$cid=R::store($copied);
				$product->typecode=4;
				$pid=R::store($product);
			}
		}
		
		function copy()
		{$id=getFormValue('id');
			if(isset($id))
			{
				$product=R::load('product',$id);
				$copied=R::dup( $product );
                                $copied->sku=$product->sku.'-Copy';
				$copied->seoid=$product->seoid.'-copy';
				$copied->name=$product->name.'-Copy';
				$cid=R::store($copied);
			}	
			
		}
	}